package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer saveComment(Comment comment) {
       //1. 保存comment对象
        mongoTemplate.save(comment);
        //2. 更新动态详情数量
        switch (comment.getCommentType()){
            case 1: { //动态点赞
                // i 查询动态详情
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //  ii数量+1
                movement.setLikeCount(movement.getLikeCount()+1);
                mongoTemplate.save(movement);
                // iii.返回
                return movement.getLikeCount();
            }
            case 2: { //动态评论
                // i查询动态详情
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                // ii 数量+1
                movement.setCommentCount(movement.getCommentCount()+1);
                mongoTemplate.save(movement);
                // iii 返回
                return movement.getCommentCount();
            }
            case 3: { //动态喜欢
                // i 查询动态详情
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                // ii 数量+1
                movement.setLoveCount(movement.getLoveCount()+1);
                mongoTemplate.save(movement);
                // iii返回
                return movement.getLikeCount();
            }
            case 4: { //视频点赞

            }
            case 5:{ //视频喜欢

            }
            case 6: { //评论点赞

            }
        }
        return null;
    }

    @Override
    public Integer removeComment(Long userId, ObjectId publishId, Integer commentType) {
       //1. 根据三个条件删除
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("publishId").is(publishId)
                        .and("commentType").is(commentType)
        );
        mongoTemplate.remove(query,Comment.class);
        //2. 跟新动态详情数量
        switch (commentType) {
            case 1: { // 动态点赞
                // i 查询动态详情
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                // ii. 数量-1
                movement.setLikeCount(movement.getLikeCount()-1);
                mongoTemplate.save(movement);
                // iii 返回数量
                return movement.getLikeCount();
            }
            case 2:{ //动态评论
                // i 查询动态详情
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                // i 数量-1
                movement.setCommentCount(movement.getCommentCount()-1);
                mongoTemplate.save(movement);
                // ii 返回数量
                return movement.getCommentCount();
            }
            case 3: { //动态喜欢
                // i 查询动态详情
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                // ii 数量 -1
                movement.setLoveCount(movement.getLoveCount()-1);
                mongoTemplate.save(movement);
                // iii 返回数量
                return movement.getLoveCount();
            }
            case 4: {

            }
            case 5: {

            }
            case 6: {

            }
        }
        return null;
    }

    // 分页查询
    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer commentType, Integer pageNum, Integer pageSize) {
       //1. 构建条件
        Query query = new Query(
                Criteria.where("publishId").is(publishId)
                        .and("commentType").is(commentType)
        ).with(Sort.by(Sort.Order.desc("create")));//排序
        //2 查询list
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        //3. 查询count
        long counts = mongoTemplate.count(query, Comment.class);
        //4. 返回并封装
        return new PageBeanVo(pageNum,pageSize,counts,commentList);
    }

    @Override
    public PageBeanVo findUserCommentByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        //1. 构建条件
        Query query = new Query(
                Criteria.where("publishUserId").is(publishUserId)
                        .and("commentType").is(commentType)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        //2. 查询list
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        //3. 返回并封装分页对象
        return new PageBeanVo(pageNum,pageSize,0L,commentList);

    }
}
